using System.Collections.Generic;
using System.Linq;
using SkaldRogueLike.Main.CellContent.Items;
using SkaldRogueLike.Main.Common;
using SkaldRogueLike.Main.Constants;

namespace SkaldRogueLike.Main.CellContent.Creatures
{
    public class Inventory
    {
        private SortedListOfItems Bag { get; set; }

        public Inventory()
        {
            Bag = new SortedListOfItems();
        }

        public void PutInBag(Item item)
        {
            if (item is Consumable)
                PutConsumable(item as Consumable);
            else
                Bag.Add(item);
        }

        private void PutConsumable(Consumable equipment)
        {
            var old = Bag.Where(eq => eq is Consumable).OfType<Consumable>()
                .FirstOrDefault(it => it.Equals(equipment));
            if (old != null)
                old.Add(equipment);
            else
                Bag.Add(equipment);
        }

        public List<Item> GetItemsFromBag()
        {
            return Bag.ToList();
        }

        public List<Equipment> GetItemsOfTypeEquipmentFromBag()
        {
            return Bag.OfType<Equipment>().ToList();
        }

        public void UpdateBagAfterEquipping(Equipment puttedOn, List<Equipment> puttedOff)
        {
            Bag.Remove(puttedOn);
            Bag.AddRange(puttedOff.FindAll(eq => eq != null));
        }

        public void Drop(Item item)
        {
            Bag.Remove(item);
        }
    }
}